package cosc617_blackteam;

/**
 * This class is used to generate unique ids in place of database generated ids.
 * the id's only persist as long as the object is active.
 *
 * @author Anton
 */
public class IdGenerator
{

    private int startIndex = 0;
    private int maxIndex = 0;
    private int currentIndex = 0;

    public IdGenerator()
    {
    }

    /**
     *
     * @param startIndex optional begin index to allow for categorizing ids for
     * different object groups. For instance start at 0 for employees, start at
     * 100 for customers and start at 1000 for orders
     */
    public IdGenerator(int startIndex)
    {
        this.startIndex = startIndex;
        this.currentIndex = startIndex;
    }

    /**
     *
     * @param startIndex begin index to allow for categorizing ids for different
     * object groups. For instance start at 0 for employees, start at 100 for
     * customers and start at 1000 for orders
     * @param maxIndex the max index for unique ids when using a start index. 0
     * (zero) means "infinite" and is the default. The calling code must handle
     * the resetting and checking for hasNextId()
     */
    public IdGenerator(int startIndex, int maxIndex)
    {
        this(startIndex);
        this.maxIndex = maxIndex;
    }

    public int getNextId()
    {
        return this.currentIndex++;
    }

    public boolean hasNextId()
    {
        if(this.maxIndex > 0 && this.maxIndex == this.currentIndex) {
            return false;
        }
        return true;
    }

    /**
     * This can be used to reset the current index for ids that have reached the
     * maxIndex threshold.
     */
    public void resetIndex()
    {
        this.currentIndex = this.startIndex;
    }
}
